<template>

    <el-tabs v-model="activeTabName" class="store-tabs" @tab-click="">
        <el-tab-pane label="基本信息" name="first">
            <EditBase />
        </el-tab-pane>

        <el-tab-pane label="微信小程序" name="second">
            <EditWechatXCX />
        </el-tab-pane>
        <el-tab-pane label="App设置" name="third">
            <EditApp />
        </el-tab-pane>
    </el-tabs>

    <el-button type="primary" @click="save">保存</el-button>
</template>

<script lang="ts" setup>

import { ref, reactive, provide } from 'vue';
import axios from 'axios';
import { Plus } from '@element-plus/icons-vue'
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

import EditBase from './Store/Base.vue'
import EditWechatXCX from './Store/WechatXCX.vue'
import EditApp from './Store/App.vue'
import { trim } from 'lodash';
const VITE_URL = window.VITE_URL;
const activeTabName:any = ref('first')

const settings:any = ref<Array<object>>([])

const setting_data:any = ref<object>({
    config: {
        shop_name: '',
        phone: '',
        qq: '',
        wechat: '',
        // home_live_show: 1,
        latlng_to_addr_ley: '', // 经纬度转详细地址KEY(后台自提点使用)
        customer_group_id: 0,
    },
    xcx: {
        shop_name: '', //适用于小程序的商城名称
        app_id: '',
        app_secret: '',
        mch_id: '',
        mch_key: '',
        location_key: '',
        float_nav_show: '0',
        apiclient_key_pem:'',
        apiclient_cert_pem:'',
    },
    app: {
        wx_app_id: '',
        wx_app_secret: '',
    }

});
const setting_xcx:any = ref<object>({

})
provide('settings', settings)
provide('setting_data', setting_data)

const get_setting_info = () => {

    const url: string = VITE_URL + '/admin.setting/index'
    axios.get(url).then(res => {

        settings.value = res.data
        settings.value.forEach((element:any) => {

            if( typeof setting_data.value[element.code] != 'undefined' ){
                setting_data.value[element.code][element.key] = element.value
            }

        });
        setting_data.value.config.customer_group_id = parseInt( setting_data.value.config.customer_group_id );
    })

}


const save = () => {

    const url: string = VITE_URL + '/admin.setting/edit'


    let data = "";

    // var index = 0;
    // for (var p in settings.value) {
    //     var code = settings.value[p];

    //     for (var q in code) {
    //         data += "&settings[" + index + "][code]=" + p;
    //         data += "&settings[" + index + "][key]=" + q;
    //         data += "&settings[" + index + "][value]=" + code[q];

    //         // data += "&settings[" + i + "][serialized]=0";

    //         index++;
    //     }
    // }

    let index = 0;
    for (let p in setting_data.value) {

        let el_group = setting_data.value[p]
        for (let q in el_group) {

            data += "&settings[" + index + "][code]=" + p;
            data += "&settings[" + index + "][key]=" + q;
            data += "&settings[" + index + "][value]=" + el_group[q]

            index++;
        }

    }

    data = trim(data, '&');
    // console.log(data); return;

    axios.post(url, data).then(res => {

        ElMessage({
            message: res.data.status == 0 ? '保存成功' : res.data.message,
            duration: 1000,
            type: res.data.status == 0 ? 'success' : 'error',
        })

    });

}

const init = () => {


    get_setting_info();

}
init();

</script>